from django.shortcuts import render
from rest_framework import response, status
from rest_framework.response import Response

from rest_framework.views import APIView
import jwt

from apps.index.models import IndexUser
from apps.index.serializers import IndexUserSerializers
from apps.list.models import ListInfo
from apps.list.serializers import ListInfoSerializer
from apps.match import models
from apps.match.models import MatchMeal, MatchFood, MatchStaple
from apps.match.serializers import MatchSerializer, MatchMealSerializer, MatchStapleSerializer


class MatchView(APIView):
    def post(self,request):
        print('match-------')
        token = self.request.META.get('HTTP_TOKEN')
        print(token)
        if not token:
            return Response({'errno': 2, 'errmsg': '未登录'})
        user = jwt.decode(token, verify=False)
        print(user['user_id'])

        # user_id = IndexUser.objects.get(id=user['user_id'])
        # serializer_user = IndexUserSerializers(user_id,many=True)
        # print(serializer_user.data)
        # list_id = IndexUser.objects.filter(id=user_id)
        # serializer_list = ListInfoSerializer(list_id)
        # print(serializer_list.data)


        # 查询所有食物
        match_info = MatchFood.objects.all()
        print(match_info)
        # 序列化食物
        serializer = MatchSerializer(match_info,many=True)
        # 序列化食物列表的第一个下的id  ---1
        print(serializer.data[0:3])
        print('-----------------------------------')
        # 查询食物表关联的id为1的搭配表
        match_meal1 = MatchMeal.objects.filter(id=serializer.data[0]['id'])
        # match_meal1 = MatchMeal.objects.all()
        # print(match_meal1)
        serializer2 = MatchMealSerializer(match_meal1,many=True)
        print(serializer2.data)
        print('------------------------------------')
        # 查询搭配表关联的id为1的三餐分类表
        match_staple1 = MatchStaple.objects.filter(id=serializer2.data[0]['id'])
        serializer3 = MatchStapleSerializer(match_staple1, many=True)
        print(serializer3.data)
        data={
            'data':serializer.data,
            'data2':serializer2.data
        }
        return Response(data,status=status.HTTP_201_CREATED)